package com.system.presideover.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.system.common.result.Result;
import com.system.presideover.entity.Emcee;
import com.system.presideover.entity.Schedule;
import com.system.presideover.service.EmceeService;
import com.system.presideover.service.ScheduleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 档期设置 前端控制器
 * </p>
 *
 * @author system
 * @since 2024-04-16
 */
@RestController
@RequestMapping("/presideover/schedule")
public class ScheduleController {

    @Autowired
    private ScheduleService scheduleService;

    @Autowired
    private EmceeService emceeService;



    @ApiOperation(value = "所有档期")
    @GetMapping("/listSchedule")
    public Result listSchedule(){
        List<Schedule> list = scheduleService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "司仪的所有档期查看")
    @GetMapping("/listSchedule/{emceeId}")
    public Result listScheduleById(@PathVariable Integer emceeId){

        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Schedule::getEmceeid,emceeId);
        List<Schedule> list = scheduleService.list(wrapper);
        return Result.ok(list);
    }

    @ApiOperation(value = "添加档期")
    @PostMapping("/saveSchedule/{emceeId}")
    public Result saveSchedule(@RequestBody Schedule accountNumber,@PathVariable Integer emceeId){

        accountNumber.setCreateTime(LocalDateTime.now());
        accountNumber.setEmceeid(emceeId);
        boolean save = scheduleService.save(accountNumber);

        if (save){
            return Result.ok().message("添加成功");
        }
        return Result.fail().message("添加失败");
    }


    @ApiOperation(value = "修改档期")
    @PostMapping("/updateSchedule")
    public Result updateSchedule(@RequestBody Schedule accountNumber){

        accountNumber.setUpdateTime(LocalDateTime.now());
        boolean save = scheduleService.updateById(accountNumber);

        if (save){
            return Result.ok().message("修改成功");
        }
        return Result.fail().message("修改失败");
    }

    @ApiOperation(value = "删除档期")
    @PostMapping("/delectSchedule")
    public Result delectSchedule(@RequestBody Schedule accountNumber){
        boolean save = scheduleService.removeById(accountNumber.getId());

        if (save){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "筛选档期")
    @GetMapping("/filterSchedule")
    public Result filterSchedule(Schedule schedule){


        List<Emcee> allEmceeById = emceeService.getAllEmceeById(schedule.getScheduleTime(), schedule.getSchedule());
        return Result.ok(allEmceeById);

//        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
//        wrapper.eq(Schedule::getScheduleTime,schedule.getScheduleTime()).eq(Schedule::getSchedule,schedule.getSchedule());
//        List<Schedule> list = scheduleService.list(wrapper);
//        List<Emcee> list2 = null;
//        for (int i = 0; i < list.size(); i++) {
//            Integer emceeid = list.get(i).getEmceeid();
//            LambdaQueryWrapper<Emcee> emceeLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            emceeLambdaQueryWrapper.eq(Emcee::getId,emceeid);
//            list2 = emceeService.list(emceeLambdaQueryWrapper);
//        }
//        return Result.ok(list2);

//        .ne(Schedule::getScheduleTime,schedule.getScheduleTime()).and()
//                .ne(Schedule::getSchedule,schedule.getSchedule());

//        LambdaQueryWrapper<Schedule> wrapper = new LambdaQueryWrapper<>();
//        QueryWrapper<Schedule> queryWrapper = new QueryWrapper<>()
//
//        QueryWrapper<Schedule> wrapper = new QueryWrapper<>();
//        wrapper.eq("schedule_time",schedule.getScheduleTime()).eq("schedule",schedule.getSchedule());
//
//        List<Schedule> list = scheduleService.list(wrapper);
//        List<Emcee> list2 = null;
//        for (int i = 0; i < list.size(); i++) {
//
//            Integer emceeid = list.get(i).getEmceeid();
//            LambdaQueryWrapper<Emcee> emceeLambdaQueryWrapper = new LambdaQueryWrapper<>();
//            emceeLambdaQueryWrapper.eq(Emcee::getId,emceeid);
//            list2 = emceeService.list(emceeLambdaQueryWrapper);
//
////            QueryWrapper<Schedule> wrapper1 = new QueryWrapper<>();
////            wrapper1.ne("schedule_time",schedule.getScheduleTime()).ne("schedule",schedule.getSchedule());
////            list2 =  emceeService.list(emceeLambdaQueryWrapper);
//        }
//
//        return Result.ok(list2);



    }

}

